/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.wind.roof;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.GenericBracingListBox;
import gov.fema.hazus.bdc.client.hazards.wind.WindConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.Roof;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;

public class WindRoofForm extends AbstractForm implements BuildingUpdate {

	private Label slope;
	private Label covering;
	private Label shape;
	private Label gableEndBracing;
	private Label sheathing;
	private Label nailSize;
	private Label nailSpacing;
	private Label deckAttachment;
	private Label frameSpacing;
	private Label roofWallAnchorage;
	private Label roofPerimeter;
	
	// form fields
	private WindRoofSlopeListBox formSlope = new WindRoofSlopeListBox();
	private WindRoofCoveringListBox formCovering = new WindRoofCoveringListBox();
	private WindRoofShapeListBox formShape = new WindRoofShapeListBox();
	private GenericBracingListBox formGableEndBracing = new GenericBracingListBox();
	private WindRoofSheathingListBox formSheathing = new WindRoofSheathingListBox();
	private WindNailSizeListBox formNailSize = new WindNailSizeListBox();
	private WindNailSpacingListBox formNailSpacing = new WindNailSpacingListBox();
	private WindMetalDeckAttachmentListBox formDeckAttachment = new WindMetalDeckAttachmentListBox();
	private WindFrameSpacingListBox formFrameSpacing = new WindFrameSpacingListBox();
	private WindRoofWallAnchorageListBox formRoofWallAnchorage = new WindRoofWallAnchorageListBox();
	private WindRoofPerimeterListBox formRoofPerimeter = new WindRoofPerimeterListBox();
	
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(11,2);
	
	// entities
	private Roof roof;
	
	// listeners
	private WindRoofListBoxChangeListener windRoofListBoxChangeListener;
	private WindConstants constants;
	
	public WindRoofForm(Building building) {

		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (WindConstants)GWT.create(WindConstants.class);
		tabTitle = constants.windRoofTabTitle();
		
		slope = new Label(constants.windRoofSlope());
		covering = new Label(constants.windRoofCovering());
		shape = new Label(constants.windRoofShape());
		gableEndBracing = new Label(constants.windRoofGableEndBracing());
		sheathing = new Label(constants.windRoofSheathing());
		nailSize = new Label(constants.windRoofNailSize());
		nailSpacing = new Label(constants.windRoofNailSpacing());
		deckAttachment = new Label(constants.windRoofDeckAttachment());
		frameSpacing = new Label(constants.windRoofFrameSpacing());
		roofWallAnchorage = new Label(constants.windRoofRoofWallAnchorage());
		roofPerimeter = new Label(constants.windRoofPerimeter());
		
		// add labels to form
		table.setWidget(0, 0, slope);
		table.setWidget(1, 0, covering);
		table.setWidget(2, 0, shape);
		table.setWidget(3, 0, gableEndBracing);
		table.setWidget(4, 0, sheathing);
		table.setWidget(5, 0, nailSize);
		table.setWidget(6, 0, nailSpacing);
		table.setWidget(7, 0, deckAttachment);
		table.setWidget(8, 0, frameSpacing);
		table.setWidget(9, 0, roofWallAnchorage);
		table.setWidget(10, 0, roofPerimeter);
		
		// set up form fields
		roof = building.getRoof();
		windRoofListBoxChangeListener = new WindRoofListBoxChangeListener(roof);
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formSlope);
		table.setWidget(1, 1, formCovering);
		table.setWidget(2, 1, formShape);
		table.setWidget(3, 1, formGableEndBracing);
		table.setWidget(4, 1, formSheathing);
		table.setWidget(5, 1, formNailSize);
		table.setWidget(6, 1, formNailSpacing);
		table.setWidget(7, 1, formDeckAttachment);
		table.setWidget(8, 1, formFrameSpacing);
		table.setWidget(9, 1, formRoofWallAnchorage);
		table.setWidget(10, 1, formRoofPerimeter);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);	

	}

	public void updateBuilding(Building building) {
		
		// entities
		setBuilding(building);
		this.roof = building.getRoof();
		
		// listeners
		windRoofListBoxChangeListener.updateRoof(roof);
		
		// reload forms
		loadForms();
		
	}

	private void loadForms()
	{
		
		if( roof != null)
		{
			formSlope.setListItem(roof.getSlope());
			formSlope.setName(Roof.SLOPE);
			
			formCovering.setListItem(roof.getCovering());
			formCovering.setName(Roof.COVERING);
			
			formShape.setListItem(roof.getShape());
			formShape.setName(Roof.SHAPE);
			
			formGableEndBracing.setListItem(roof.getGableEndBracing());
			formGableEndBracing.setName(Roof.GABLEENDBRACING);
			
			formSheathing.setListItem(roof.getSheathing());
			formSheathing.setName(Roof.SHEATHING);
			
			formNailSize.setListItem(roof.getNailSize());
			formNailSize.setName(Roof.NAILSIZE);
			
			formNailSpacing.setListItem(roof.getNailSpacing());
			formNailSpacing.setName(Roof.NAILSPACING);
			
			formDeckAttachment.setListItem(roof.getDeckAttachment());
			formDeckAttachment.setName(Roof.DECKATTACHMENT);
			
			formFrameSpacing.setListItem(roof.getFrameSpacing());
			formFrameSpacing.setName(Roof.FRAMESPACING);
			
			formRoofWallAnchorage.setListItem(roof.getRoofWallAnchorage());
			formRoofWallAnchorage.setName(Roof.ROOFWALLANCHORAGE);
			
			formRoofPerimeter.setListItem(roof.getRoofPerimeter());
			formRoofPerimeter.setName(Roof.ROOFPERIMETER);
		}
		
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formSlope.setEnabled(false);
	            formCovering.setEnabled(false);
	            formShape.setEnabled(false);
	            formGableEndBracing.setEnabled(false);
	            formSheathing.setEnabled(false);
	            formNailSize.setEnabled(false);
	            formNailSpacing.setEnabled(false);
	            formDeckAttachment.setEnabled(false);
	            formFrameSpacing.setEnabled(false);
	            formRoofWallAnchorage.setEnabled(false);
	            formRoofPerimeter.setEnabled(false);
	        }
	        else
	        {
	            formSlope.setEnabled(true);
	            formCovering.setEnabled(true);
	            formShape.setEnabled(true);
	            formGableEndBracing.setEnabled(true);
	            formSheathing.setEnabled(true);
	            formNailSize.setEnabled(true);
	            formNailSpacing.setEnabled(true);
	            formDeckAttachment.setEnabled(true);
	            formFrameSpacing.setEnabled(true);
	            formRoofWallAnchorage.setEnabled(true);
	            formRoofPerimeter.setEnabled(true);            
	        }
		}
	}
	
	private void loadListeners()
	{
		formSlope.addChangeListener(windRoofListBoxChangeListener);
		formCovering.addChangeListener(windRoofListBoxChangeListener);
		formShape.addChangeListener(windRoofListBoxChangeListener);
		formGableEndBracing.addChangeListener(windRoofListBoxChangeListener);
		formSheathing.addChangeListener(windRoofListBoxChangeListener);
		formNailSize.addChangeListener(windRoofListBoxChangeListener);
		formNailSpacing.addChangeListener(windRoofListBoxChangeListener);
		formDeckAttachment.addChangeListener(windRoofListBoxChangeListener);
		formFrameSpacing.addChangeListener(windRoofListBoxChangeListener);
		formRoofWallAnchorage.addChangeListener(windRoofListBoxChangeListener);
		formRoofPerimeter.addChangeListener(windRoofListBoxChangeListener);
	}
}
